package org.moocgen.web;

import java.util.List;

import org.moocgen.model.User;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.roo.addon.web.mvc.controller.json.RooWebJson;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/users")
@Controller
@RooWebScaffold(path = "users", formBackingObject = User.class)
@RooWebJson(jsonObject=User.class)
public class UserController 
{
	//////////////////////////////////////////////////////////////
	@RequestMapping(method = RequestMethod.POST, headers = "Accept=application/json")
    public ResponseEntity<String> createFromJson(	@RequestBody String json,
    												@RequestParam(value = "callback", required = false) String callback) 
    {
        User user = User.fromJsonToUser(json);
        userService.saveUser(user);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        String call = "";
        if(callback!=null) call = callback+"("+user.toJson()+")";
        else call = user.toJson();
        
        return new ResponseEntity<String>(call, headers, HttpStatus.CREATED);
    }
	    
	//////////////////////////////////////////////////////////////
	@RequestMapping(headers = "Accept=application/json")
    @ResponseBody
    public ResponseEntity<String> listJson(@RequestParam(value = "callback", required = false) String callback) 
    {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json; charset=utf-8");
        List<User> result = userService.findAllUsers();
        
        String call = "";
        if(callback!=null) call = callback+"("+User.toJsonArray(result)+")";
        else call = User.toJsonArray(result);
        
        return new ResponseEntity<String>(call, headers, HttpStatus.OK);
    }
    
	//////////////////////////////////////////////////////////////
	@RequestMapping(value = "/{matNum}", headers = "Accept=application/json")
    @ResponseBody
    public ResponseEntity<String> showJson(	@PathVariable("matNum") String matNum,
    										@RequestParam(value = "callback", required = false) String callback) 
    {
        User user = userService.findUser(matNum);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json; charset=utf-8");
        
        if (user == null) 
        {
            return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
        }
       
        String call = "";
        if(callback!=null) call = callback+"("+user.toJson()+")";
        else call = user.toJson();
        
        return new ResponseEntity<String>(call, headers, HttpStatus.OK);
    }
    
	//////////////////////////////////////////////////////////////
	@RequestMapping(value = "/{matNum}", method = RequestMethod.DELETE, headers = "Accept=application/json")
    public ResponseEntity<String> deleteFromJson(@PathVariable("matNum") String matNum,
    											 @RequestParam(value = "callback", required = false) String callback) 
    {
        User user = userService.findUser(matNum);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
       
        if (user == null) 
        {
            return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
        }
        
        userService.deleteUser(user);
        String call = "";
        if(callback!=null) call = callback+"("+user.toJson()+")";
        else call = user.toJson();
        
        return new ResponseEntity<String>(call, headers, HttpStatus.OK);
   }
}
